package com.zhf.controller;

import com.zhf.domain.Menu;
import com.zhf.domain.ResponseResult;
import com.zhf.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/12/5 13:41
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /*
    查询所有菜单
     */
    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu(){
        List<Menu> allMenu = menuService.findAllMenu();
        return  new ResponseResult(true,200,"查询所有菜单信息成功",allMenu);
    }

    /*
    回显菜单信息
     */
    @RequestMapping("/findMenuInfoById")
    public ResponseResult findMenuInfoById(int id) {
        //根据id的值判断当前是更新还是添加
        if (id==-1){
            //添加操作 回显信息中不需要查询menu信息
            List<Menu> subMenuListByPid = menuService.findSubMenuListByPid(-1);
            HashMap<String, Object> map = new HashMap<>();
            map.put("menuInfo",null);
            map.put("parentMenuList",subMenuListByPid);
            ResponseResult result = new ResponseResult(true, 200, "添加回显成功", map);
            return result;
        }else {
            Menu menu=menuService.findMenuById(id);
            HashMap<String, Object> map = new HashMap<>();
            map.put("menuInfo",null);
            map.put("parentMenuList",menu);
            ResponseResult result = new ResponseResult(true, 200, "修改回显成功", map);
            return result;
        }
    }
}
